###############################################################
###############################################################
## Questo Makefile controlla il build e la generazione della  ##
## documentazione della librariea libfat                      ##
###############################################################
###############################################################

# Specifica dove cercare i file di include (in ./Include)
vpath %.h ./Include

# Specifica dove cercare i file oggetto (in ./bin)
vpath %.o ../bin

# Specifica dove installare la libreria
BIN = ../bin

LIBDIR = ../lib

# Directory dei file di include
INC = ./Include

# usiamo gcc
CC = gcc

# Path per gli include files
CPPFLAGS = -I $(INC)

# Path librerie
LIBS = -L $(LIBDIR)

# Compilation flag (con informazioni di coverage)
CFLAGS = -Wall -pedantic -g #-fprofile-arcs -ftest-coverage 

# Direcotry dove risiede la documentazione di libfat
FAT_DOC = ../Documentation

# nome libreria
LIBNAME = libfat.a

# Lista degli object files
OBJS = 	format.o \
	load_fat.o \
	make_dir.o \
	util.o \
	list.o \
	open_fat.o \
	write_fat.o \
	read_fat.o \
	sfaterror.o 


.PHONY: clean lib doc install test

# Target per la creazione della libreria libfat
lib: $(OBJS)
	cd $(BIN); ar -r $(LIBNAME) $(OBJS)

# Target di installazione nella directory $(LIBS)
install: $(BIN)/$(LIBNAME)
	cd $(BIN); cp $(BIN)/$(LIBNAME) $(LIBDIR)

# Target per la compilazione generazione dei file oggetto da inserire in libfat
# DA AGGIORNARE CON LE DIPENDENZE GIUSTE
format.o: format.c fat.h fat_defs.h format.h
	$(CC) $(CFLAGS) $(CPPFLAGS)  -c -o $(BIN)/$@  $<

list.o: list.c fat.h fat_defs.h list.h  util.h
	$(CC) $(CFLAGS) $(CPPFLAGS)  -c -o $(BIN)/$@  $<

load_fat.o: load_fat.c fat.h fat_defs.h load_fat.h
	$(CC) $(CFLAGS) $(CPPFLAGS)  -c -o $(BIN)/$@  $<

make_dir.o: make_dir.c fat.h fat_defs.h make_dir.h util.h
	$(CC) $(CFLAGS) $(CPPFLAGS)  -c -o $(BIN)/$@  $<

read_fat.o: read_fat.c fat.h fat_defs.h read_fat.h util.h
	$(CC) $(CFLAGS) $(CPPFLAGS)  -c -o $(BIN)/$@  $<

open_fat.o: open_fat.c fat.h fat_defs.h util.h
	$(CC) $(CFLAGS) $(CPPFLAGS)  -c -o $(BIN)/$@  $<

util.o: util.c fat.h fat_defs.h util.h
	$(CC) $(CFLAGS) $(CPPFLAGS)  -c -o $(BIN)/$@  $<

write_fat.o: write_fat.c fat.h fat_defs.h write_fat.h util.h
	$(CC) $(CFLAGS) $(CPPFLAGS)  -c -o $(BIN)/$@  $<

sfaterror.o: sfaterror.c fat_defs.h
	$(CC) $(CFLAGS) $(CPPFLAGS)  -c -o $(BIN)/$@  $<


# Clean up target
clean:
	-rm -f *~
	-rm -f ./*.o
	-rm -fr ./*.gc*
	-rm -f $(BIN)/*.o
	-rm -fr $(BIN)/*.gc*
	-rm -fr $(BIN)/*.a

# Target per generare la documentazione 
doc:
	$(MAKE) --directory=$(FAT_DOC)

# target di test automatico (test cunit directory ../Test) 
# per il testing incrementale commentare quelli 
# che interessano
test:
	make clean
	make lib
	make install
	@echo "Testing format ..."
	make -C ../Test/FormatTest test
	@echo "Testing mkdir ..."
	make -C ../Test/MkdirTest test	
	@echo "Testing open (mkfile) ..."
	make -C ../Test/OpenTest test
	@echo "Testing read/write ..."
	make -C ../Test/RW_Test test
	@echo "Testing all ..."
	make -C ../Test/TestAll test
	@echo -e "\n\n\a *** Test LIBFAT COMPLETA superato! ***\n\n"

